import { dialog } from 'electron'

import type * as pojo from 'shared-types'
import { useWebsocket } from './websocket'
import { defineBean } from '../util'

enum Id {
  SYS = 1,
  NOTICE,
  COOKIE
}

enum NoticeSubId {
  RECEIVE_NOTICE = 1,
  LIST_NOTICE ,
  READ_NOTICE
}

enum CookieSubId {
  LIST_PLATFORMS = 1,
  LIST_PAGES,
  LIST_COOKIES,
  UPDATE_COOKIES
}

function createRepository() {
  const ws = useWebsocket()

  function login(query: pojo.LoginQuery): Promise<pojo.LoginRepoVo> {
    return ws.sendHandShake(query)
  }
  function onNotice(query: pojo.OnNoticeQuery) {
    ws.receiveMsg(Id.NOTICE, NoticeSubId.RECEIVE_NOTICE, query)
  }
  function listNotices(): Promise<pojo.ListNoticesVo> {
    return ws.sendMsg(Id.NOTICE, NoticeSubId.LIST_NOTICE)
  }
  function readNotice(query: pojo.ReadNoticeQuery) {
    return ws.sendMsg(Id.NOTICE, NoticeSubId.READ_NOTICE, query)
  }
  function listPlatforms(): Promise<pojo.ListPlatformsVo> {
    return ws.sendMsg(Id.COOKIE, CookieSubId.LIST_PLATFORMS)
  }
  function listPages(): Promise<pojo.ListPagesVo> {
    return ws.sendMsg(Id.COOKIE, CookieSubId.LIST_PAGES)
  }
  function listCookies(query: pojo.ListCookiesQuery): Promise<pojo.ListCookiesVo> {
    return ws.sendMsg(Id.COOKIE, CookieSubId.LIST_COOKIES, query)
  }
  function updateCookies(query: pojo.UpdateCookiesQuery): Promise<pojo.UpdateCookiesVo> {
    return ws.sendMsg(Id.COOKIE, CookieSubId.UPDATE_COOKIES, query)
  }

  return {
    login,
    listPlatforms,
    listPages,
    onNotice,
    listNotices,
    readNotice,
    listCookies,
    updateCookies
  }
}

export const useRepository = defineBean(createRepository)
